package com.haierp.controller;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.haierp.mapper.ShippingOrderMapper;
import com.haierp.model.FileManage;
import com.haierp.service.IFileManageService;
import com.haierp.service.IUploadFileService;
import com.haierp.util.AnalysisExcelUtil;
import com.haierp.util.DateUtil;
import com.haierp.util.JsonResult;
import com.haierp.vo.FileManageQueryVO;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.shiro.ShiroUser;
import com.wangzhixuan.mapper.OrganizationMapper;
import com.wangzhixuan.model.Organization;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/fileManage")
public class FileManageController extends BaseController {
	@Autowired
	private IFileManageService fileManageService;
	@Autowired
	private OrganizationMapper organizationMapper;

	@Autowired
	private IUploadFileService uploadFileService;

	@Autowired
	private ShippingOrderMapper shippingOrderMapper;

	@RequestMapping("/addFile")
	@ResponseBody
	public Object addFile(FileManage fileManage){
		JsonResult<String> result = new JsonResult<>();
		if(fileManage.getOrganizationId() == 11){
			String fileUrl = fileManage.getFileUrl();
			ShiroUser one = this.getShiroUser();
			fileManage.setCompanyId(one.getCompanyId());
			fileManage.setCreateBy(one.getLoginName());
			fileManage.setFileName(fileManage.getFileName());
			fileManage.setFileUrl(fileUrl);
			fileManage.setGmtCreate(new Date());
			fileManage.setGmtModify(new Date());
			fileManage.setOrganizationId(fileManage.getOrganizationId());
			fileManage.setOrganizationName("打包部门");
			fileManageService.insert(fileManage);
			JSONObject fromObject = JSONObject.fromObject(fileUrl);
			Object object = fromObject.get("allFileList");
			JSONArray fromObject2 = JSONArray.fromObject(object);
			String urlTemp = null;
			for (Object object2 : fromObject2) {
				JSONObject fromObject3 = JSONObject.fromObject(object2);
				urlTemp = (String)fromObject3.get("url");
			}
			List<List<Object>> allRows = AnalysisExcelUtil.analysisExcel(urlTemp);
			for (int i = 0; i < allRows.size(); i++) {
				List<Object> rowDataTemp = allRows.get(i);
				shippingOrderMapper.updatePackage(rowDataTemp.get(0).toString(), rowDataTemp.get(1).toString());
			}
		} else {
			fileManage.setGmtCreate(new Date());
			fileManage.setGmtModify(new Date());
			Organization organization = organizationMapper.selectById(fileManage.getOrganizationId());
			fileManage.setOrganizationName(organization.getName());
			ShiroUser shiroUser = this.getShiroUser();
			fileManage.setCompanyId(shiroUser.getCompanyId());
			fileManage.setCreateBy(shiroUser.getLoginName());
			fileManageService.insert(fileManage);
		}
		return result.buildIsSuccess(true);
	}

	@RequestMapping("/deleteFile")
	@ResponseBody
	public Object deleteFile(Long fileManageId){
		JsonResult<String> result = new JsonResult<>();
		FileManage selFile = fileManageService.selectById(fileManageId);
		String urlStr = selFile.getFileUrl();
		JSONObject jsonObject = JSONObject.fromObject(urlStr);
		List<Object> objectList = (List<Object>) jsonObject.get("allFileList");
		for (Object s: objectList) {
			JSONObject jsonUrl = JSONObject.fromObject(s);
			String url = (String) jsonUrl.get("url");
			uploadFileService.fileDelete(url);
		}
		jsonObject.get("allFileList");

		fileManageService.deleteById(fileManageId);

		return result.buildIsSuccess(true);
	}

	@RequestMapping("/modifyFile")
	@ResponseBody
	public Object modifyFile(FileManage fileManage){
		JsonResult<String> result = new JsonResult<>();
		fileManage.setGmtModify(new Date());
		fileManageService.updateById(fileManage);
		return result.buildIsSuccess(true);
	}

	/**
	 * 运营部文件
	 * @param fileManageQueryVO
	 * @return
	 */
	@RequestMapping("/queryFile")
	@ResponseBody
	public Object queryFile(FileManageQueryVO fileManageQueryVO){
		JsonResult<List<FileManage>> result = new JsonResult<>();
		if(fileManageQueryVO.getStartGmt() != null) {
			String startGmt = fileManageQueryVO.getStartGmt();
			String startTime = startGmt + " 00:00:00";
			fileManageQueryVO.setStartGmt(startTime);
		}
		if(fileManageQueryVO.getEndGmt() != null) {
			String endGmtStr = fileManageQueryVO.getEndGmt();
			String endTime = endGmtStr + " 23:59:59";
			fileManageQueryVO.setEndGmt(endTime);
		}
		result = fileManageService.queryFiles(fileManageQueryVO);
		return result.buildIsSuccess(true);
	}

	/**
	 * 仓储部文件
	 * @param fileManageQueryVO
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/queryStorageFile")
	@ResponseBody
	public Object queryStorageFile(FileManageQueryVO fileManageQueryVO) throws ParseException{
		JsonResult<List<FileManage>> result = new JsonResult<>();
		if(fileManageQueryVO.getStartGmt() != null) {
			String startGmt = fileManageQueryVO.getStartGmt();
			String startTime = startGmt + " 00:00:00";
			fileManageQueryVO.setStartGmt(startTime);
		}
		if(fileManageQueryVO.getEndGmt() != null) {
			String endGmtStr = fileManageQueryVO.getEndGmt();
			String endTime = endGmtStr + " 23:59:59";
			fileManageQueryVO.setEndGmt(endTime);
		}
		result = fileManageService.queryStorageFiles(fileManageQueryVO);
		return result.buildIsSuccess(true);
	}

	/**
	 * 打包部文件
	 * @param fileManageQueryVO
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/queryPackerFile")
	@ResponseBody
	public Object queryPackerFile(FileManageQueryVO fileManageQueryVO) throws ParseException{
		JsonResult<List<FileManage>> result = new JsonResult<>();
		if(fileManageQueryVO.getStartGmt() != null) {
			String startGmt = fileManageQueryVO.getStartGmt();
			String startTime = startGmt + " 00:00:00";
			fileManageQueryVO.setStartGmt(startTime);
		}
		if(fileManageQueryVO.getEndGmt() != null) {
			String endGmtStr = fileManageQueryVO.getEndGmt();
			String endTime = endGmtStr + " 23:59:59";
			fileManageQueryVO.setEndGmt(endTime);
		}
		result = fileManageService.queryPackerFiles(fileManageQueryVO);
		return result.buildIsSuccess(true);
	}

	@RequestMapping("/queryFileById")
	@ResponseBody
	public Object queryFileById(Long fileManageId){
		JsonResult<FileManage> result = new JsonResult<>();
		FileManage fileManage = fileManageService.selectById(fileManageId);
		result.buildData(fileManage);
		return result.buildIsSuccess(true);
	}
}
